package cn.com.wxd.common.util;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * Title:
 * Description:
 * <p>
 * Company: amumu
 * Copyright: Copyright (c) 2017
 * All right reserved.
 * Created by WangXuDong on 2017/8/16.
 *
 * @author WangXuDong
 * @version 1.0
 */
public class JdbcUtil {
    private static final Logger log = LoggerFactory.getLogger(JdbcUtil.class);
    private static DataSource dataSource=null;
    static{
        dataSource=new ComboPooledDataSource("AmumuCommonDataSource");
    }

    /**
     * 获取数据库连接
     * @return conn
     */
    public static Connection getConnection(){
        Connection conn=null;
        try {
            conn=dataSource.getConnection();
        } catch (SQLException e) {
            log.error("获取连接失败"+e);
        }
        return conn;
    }


    /**
     * 关闭数据库连接
     * @param conn
     */
    public static void closeConn(Connection conn){
        try {
            if(conn!=null && conn.isClosed()){
                conn.close();
            }
        } catch (SQLException e) {
            log.error("关闭连接失败"+e);
        }
    }
}
